<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客注册页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">

</head>

<body>
    <div class="nav">
        <img src="pic/logo.jpg" alt="图片加载失败">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
    </div>

    <div class="container-login">
        <div class="login-dialog">
            <h3>注册</h3>
            <div class="row">
                <span><label for="username">用户名</label></span>
                <input type="text" name="username" id="username" placeholder="请输入用户名">
            </div>
            <div class="row">
                <span><label for="password">密码</label></span>
                <input type="password" name="password" id="password" placeholder="请输入密码">
            </div>
            <div class="row">
                <button id="submit" onclick="register()">提交</button>
            </div>
            <div class="login-tip">
                已有账号? <a href="blog_login.html">登录</a>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        function register() {
            // 1、获取输入框的值
            let username = $('#username').val();
            let password = $('#password').val();
            // 2、判断其值是否为空，并构造JSON格式的数据
            if (username == '' || password == '') {
                alert('请输入完整的注册信息');
                return;
            }
            let user = JSON.stringify({
                "userName": username,
                "password": password
            });
            // 3、发送Ajax请求
            $.ajax({
                type: "post",
                url: "/user/register",
                contentType: "application/json",
                data: user,
                // 这里的success是指HTTP请求成功，而不是指业务成功
                success(result) {
                    if (result.data) {
                        // 注册成功
                        alert('注册成功');
                        location.assign("blog_login.html");
                    } else {
                        alert(result.message);
                    }
                },
                error(xhr) {
                    alert('HTTP请求失败');
                }
            });
        }
    </script>
</body>

</html>